package org.zero.resolver;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;
import org.zero.exception.SysException;

/**
 * @author Zero
 */
public class SysExceptionResolver implements HandlerExceptionResolver {
    @Override
    public ModelAndView resolveException(HttpServletRequest request,
                                         HttpServletResponse response,
                                         Object handler,
                                         Exception ex) {
        SysException sysException = null;
        if (ex instanceof SysException) {
            sysException = (SysException) ex;
        } else {
            sysException = new SysException("发送了其他未知错误，我们正在全力排查...");
        }

        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("errMsg", sysException.getMessage());
        modelAndView.setViewName("error");
        return modelAndView;
    }
}
